package com.cn.xiaonuo.modular.service.app;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.modular.dao.UserClassmateApplyDao;
import com.cn.xiaonuo.modular.dao.UserClassmateDao;
import com.cn.xiaonuo.modular.dao.UserClassmateMsgDao;
import com.cn.xiaonuo.modular.model.app.UserClassmate;
import com.cn.xiaonuo.modular.model.app.UserClassmateApply;
import com.cn.xiaonuo.modular.model.app.UserClassmateMsg;
/**
 * 同学管理服务类
 * @author 18975
 *
 */
@Service
public class UserClassmoteService {
	
	@Autowired
	private UserClassmateApplyDao userClassmateApplyDao;
	@Autowired
	private UserClassmateDao userClassmateDao;
	@Autowired
	private UserClassmateMsgDao userClassmateMsgDao;
	
	/** 同学列表 */
	public List<UserClassmate> findUserClassmateList(Long userId){
		return userClassmateDao.createLambdaQuery()
			.andEq(UserClassmate :: getUserId, userId)
			.andEq(UserClassmate :: getStatus, "1")
			.orderBy(UserClassmate :: getCreateTime).select();
	}
	
	/** 同学申请 */
	public void addUserClassmoteApply(UserClassmateApply apply) {
		UserClassmate userClassmate = userClassmateDao.createLambdaQuery()
			.andEq(UserClassmate :: getUserId, apply.getUserId())
			.andEq(UserClassmate :: getClassmateId, apply.getClassmateId())
			.andEq(UserClassmate :: getStatus, "1").single();
		if(null != userClassmate && "1".equals(userClassmate.getStatus())) {
			throw new ServiceException(9999,"当前用户已成为您的同学！");
		}
		UserClassmateApply single = userClassmateApplyDao.createLambdaQuery()
			.andEq(UserClassmateApply :: getUserId, apply.getUserId())
			.andEq(UserClassmateApply :: getClassmateId, apply.getClassmateId()).single();
		if(null != single && "3".equals(single.getStatus())) {
			throw new ServiceException(9999,"对不起，当前用户已拒绝您的申请！");
		}
		if(null != single && "0".equals(single.getStatus())) {
			throw new ServiceException(9999,"对不起，请勿重复操作！");
		}
		apply.setUpdateTime(new Date());
		apply.setStatus("0");
		userClassmateApplyDao.insert(apply);
		UserClassmateApply single3 = userClassmateApplyDao.createLambdaQuery()
				.andEq(UserClassmateApply :: getUserId, apply.getUserId())
				.andEq(UserClassmateApply :: getClassmateId, apply.getClassmateId()).single();
		if(null == single3) {
			throw new ServiceException(9999,"对不起，当前网络异常，请重试！");
		}
		UserClassmateMsg msg = new UserClassmateMsg();
		msg.setCreateTime(new Date());
		msg.setUserId(apply.getClassmateId());
		msg.setStatus("0");
		msg.setMsgType("1");
		msg.setApplyUserId(apply.getUserId());
		msg.setAttach1(single3.getId() + "");
		userClassmateMsgDao.insert(msg);
	}
	
	/** 操作同学申请 */
	public void opUserClassmoteApply(Long msgId,Long applyId,String opType) {
		UserClassmateMsg msg = userClassmateMsgDao.single(msgId);
		UserClassmateApply apply = userClassmateApplyDao.single(applyId);
		if(null == apply || null == msg) {
			throw new ServiceException(9999,"对不起，当前网络异常，请稍后再试！");
		}
		UserClassmateMsg msg1 = new UserClassmateMsg();
		msg1.setCreateTime(new Date());
		msg1.setUserId(apply.getClassmateId());
		msg1.setStatus("0");
		msg1.setMsgType(opType);
		msg1.setApplyUserId(apply.getUserId());
		userClassmateMsgDao.insert(msg1);
		if("3".equals(opType)) {//同意申请
			UserClassmate uc = new UserClassmate();
			uc.setCreateTime(new Date());
			uc.setStatus("1");
			uc.setUserId(apply.getUserId());
			uc.setClassmateId(apply.getClassmateId());
			userClassmateDao.insert(uc);
			uc.setUserId(apply.getClassmateId());
			uc.setClassmateId(apply.getUserId());
			userClassmateDao.insert(uc);
		}
		apply.setStatus(opType);//操作类型
		userClassmateApplyDao.updateTemplateById(apply);
		msg.setStatus("1");//已处理
		userClassmateMsgDao.updateTemplateById(msg);
	}
	
	/** 操作消息 */
	public void opUserClassmateMsg(Long msgId,String op) {
		UserClassmateMsg msg = userClassmateMsgDao.single(msgId);
		if(null == msg) {
			return;
		}
		if("1".equals(op) && "1".equals(msg.getMsgType())) {//申请消息，只能操作，不能阅读
			return;
		}
		msg.setStatus(op);//已阅读
		userClassmateMsgDao.updateTemplateById(msg);
	}
	
	public void deleteUserClassmote(Long userId,Long classmateId) {
		List<UserClassmate> select = userClassmateDao.createLambdaQuery()
				.andEq(UserClassmate :: getUserId, userId)
			.andEq(UserClassmate :: getClassmateId, classmateId).select();
		if(null == select || select.isEmpty()) {
			throw new ServiceException(9999,"对不起，对方不是您的同学，不需要解除同学关系！");
		}
		userClassmateDao.deleteUserClassmate(userId, classmateId);
	}
	
	/** 消息列表 */
	public List<UserClassmateMsg> findUserClassmateMsg(Long userId,int pageNo,int pageSize) {
		return userClassmateMsgDao.createLambdaQuery()
			.andEq(UserClassmateMsg :: getUserId, userId)
			.andNotEq(UserClassmateMsg :: getStatus, "-1")
			.orderBy(UserClassmateMsg :: getCreateTime)
			.limit(pageNo, pageSize).select();
	}

}
